﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.DisjoinProForOutShop.IView;
using MINERP.BLL.DisjoinProForOutShop;
using MINERP.MODEL.FoundationModel;
using MINERP.DisjoinProForOutShop.Presenter;
using MINERP.MODEL.DisjoinProForOutShop;
using MINERP.MODEL.OverOrder;

namespace MINERP.DisjoinProForOutShop.Forms
{
    public partial class FrmSelectInfo :  System.Windows.Forms.Form,IFrmSelectInfo
    {
        public FrmSelectInfo()
        {
            InitializeComponent();
            _presenter = new FrmSelectInfoPresenter(this);
        }

        public FrmSelectInfo(List<OverOrderView> inShopInGrid, int inGridType)
        {
            InitializeComponent();
            _presenter = new FrmSelectInfoPresenter(this);
            ShopInGrid = inShopInGrid;
            CurrentGridType = inGridType;
        }
        List<OverOrderView> ShopInGrid = null;
      
        private FrmSelectInfoPresenter _presenter;

        #region IFrmSelectInfo 成员

        public int CurrShopId
        {
            get;
            set;
        }

        public DateTime StartTime
        {
            get;
            set;
        }

        public DateTime EndTime
        {
            get;
            set;
        }

        public int CurrentGridType
        {
            get;
            set;
        }

        public void BangDingShop(List<MINERP.MODEL.FoundationModel.Shop_Info> shop)
        {
            comboBoxShop.DisplayMember = "ShopName";
            comboBoxShop.ValueMember = "ShopId";
            comboBoxShop.DataSource = shop;
        }

        #endregion





        #region IFrmSelectInfo 成员


        public void DisplayListOrderView(List<OrderView> list)
        {
            if (this.ShopInGrid != null)
            {
                foreach (GridDetailBase grid in ShopInGrid)
                {
                    foreach (OrderView product in list)
                    {
                        if (product.Orderinfo.OrderProductId == grid.OrderProductId)
                        {
                            product.IsInGrid = true;
                            break;
                        }
                    }
                }
            }
            label4.Text = list.Count.ToString();
            gridControlOrder.DataSource = list;
            gridControlOrder.RefreshDataSource();
            

        }

        #endregion

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            this.CurrShopId = ((Shop_Info)comboBoxShop.SelectedItem).ShopId;
            this.StartTime = Convert.ToDateTime(dateEditOrderTime.Text==""?DateTime.Now.ToString():dateEditOrderTime.Text);
            this.EndTime = Convert.ToDateTime(dateEditendTime.Text == "" ? DateTime.Now.ToString() : dateEditendTime.Text);
            _presenter.Display();
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }

        private void FrmSelectInfo_Load(object sender, EventArgs e)
        {
            dateEditOrderTime.Text = DateTime.Now.Subtract(TimeSpan.FromDays(1.0)).ToString();
            dateEditendTime.Text = DateTime.Now.ToString();
            comboBoxShop.Text = "";
            _presenter.init();
            
        }

        private void gridControlOrder_Enter(object sender, EventArgs e)
        {
            
        }

        private void dateEditendTime_Enter(object sender, EventArgs e)
        {
            dateEditendTime.DateTime = DateTime.Now;
            dateEditendTime.Refresh();
        }

        private void dateEditOrderTime_Enter(object sender, EventArgs e)
        {
            if (dateEditOrderTime.Text == "")
            {
                dateEditOrderTime.DateTime = DateTime.Now;
                dateEditOrderTime.Refresh();
            }
            
        }

        private void gridViewOrder_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            
                if (e.RowHandle >= 0)
                {
                    OrderView view = (OrderView)gridViewOrder.GetRow(e.RowHandle);
                    DateTime FetchTime = view.FetchTime;
                    if (view.IsInGrid)
                    {
                        e.Appearance.BackColor = Color.LightBlue;
                    }
                    else
                    {
                        if (DateTime.Now.CompareTo(FetchTime) > 0)
                        {
                            e.Appearance.BackColor = Color.Red;
                        }
                    }
                }
            
        }

        //private void gridViewOrder_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        //{
        //    if (e.RowHandle >= 0)
        //    {
        //        OrderView view = (OrderView)gridViewOrder.GetRow(e.RowHandle);
        //        if (view.IsInGrid)
        //        {
        //            e.Appearance.BackColor = Color.Red;
        //        }

        //    }
        //}
    }
}
